import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/model/sim_code_entity.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

///************************
/// @auther nb
/// @function 注册
/// @创建日期 2019/8/13 14:41
///************************
class Register extends StatefulWidget {
  int type = 1;//type==1  登录注册  type==2 找回密码

  Register(this.type);

  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  int line1Color = 0xFFe5e5e5;

  FocusNode _nameFocusNode;
  TextEditingController _nameController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _nameFocusNode = FocusNode();
    _nameFocusNode.addListener(() {
      if (_nameFocusNode.hasFocus) {
        print("name 获取到焦点");
        setState(() {
          line1Color = 0xFFdf3033;
        });
      } else {
        setState(() {
          line1Color = 0xFFe5e5e5;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView(
        children: <Widget>[
          ///x
          getRow1(),

          ///登录
          getRow2(),

          ///输入手机号
          getRow4(),
          getLine1(),

          getButton(),
          getBottom(),
        ],
      ),
    );
  }

  getRow1() {
    return Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30),
          MediaQueryData.fromWindow(window).padding.top, 0, 0),
      height: ScreenUtil().setWidth(88),
      child: InkWell(
        onTap: () {
          Application.router.pop(context);
        },
        child: Container(
          padding: EdgeInsets.all(ScreenUtil().setWidth(30)),
          color: Colors.white,
          child: Image.asset(
            Utils.getImgPath("icon_delete"),
            width: ScreenUtil().setWidth(32),
            height: ScreenUtil().setWidth(32),
          ),
        ),
      ),
    );
  }

  getRow2() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(92), ScreenUtil().setWidth(88), 0, 0),
      alignment: Alignment.centerLeft,
      child: Text(
        widget.type == 1 ? "登录/注册" : "找回密码",
        style: TextStyle(
          fontSize: ScreenUtil().setWidth(48),
          color: Colours.appC1,
          fontFamily: Font.PingFangBlod,
        ),
      ),
    );
  }

  getRow4() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(90), ScreenUtil().setWidth(90), 0),
      child: TextField(
        style:TextStyles.textFiled,
        controller: _nameController,
        keyboardType: TextInputType.phone,
        inputFormatters: <TextInputFormatter>[
          WhitelistingTextInputFormatter.digitsOnly, //只输入数字
          LengthLimitingTextInputFormatter(11) //限制长度
        ],
        focusNode: _nameFocusNode,
        decoration: InputDecoration(
          hintText: "请输入手机号",
          hintStyle: TextStyles.custom(
              28, Color(0xFF999999), Font.PingFangMedium),
          border: InputBorder.none,
        ),
      ),
    );
  }

  getLine1() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      width: ScreenUtil().setWidth(750),
      color: Color(line1Color),
      height: ScreenUtil().setWidth(1),
    );
  }

  getButton() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(56),
          ScreenUtil().setWidth(96), ScreenUtil().setWidth(56), 0),
      child: UiUtils.bottomBotton("获取验证码", () {
        var phoneNum = _nameController.text;
        if (phoneNum.length != 11) {
          ToastUtil.showShortErrToast("请输入正确的手机号！");
          return;
        }
        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return new LoadingDialog(
                text: "正在发送验证码…",
              );
            });

        _getsmscode();

        ///Application.router.navigateTo(context, "${Routes.verifyCode}?phoneNum=${_nameController.text}");
      }),
    );
  }

  getBottom() {
    return Container(
      alignment: Alignment.centerRight,
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(30), ScreenUtil().setWidth(90), 0),
      child: GestureDetector(
        onTap: () {
          NavigatorUtils.push(context, Routes.login, replace: true);
        },
        child: Text(
          "密码登录",
          style: TextStyles.custom(
              24, Colours.appC11, Font.PingFangMedium),
        ),
      ),
    );
  }

  Future _getsmscode() async {
    //17099298919  bin110123
    FormData params = FormData.from(
        {"tel": _nameController.text, "type": widget.type == 1 ? "3" : "2"});
    DioManager.getInstance().post(Api.GETSMSCODE, params,
        //正常回调
        (data) {
      print("获取验www证码:$data");
      SimCodeEntity simCodeEntity = SimCodeEntity.fromJson(data);
      print(simCodeEntity.code == Api.SUCCESS_CODE);

      if (simCodeEntity.code == Api.SUCCESS_CODE) {
        Navigator.pop(context);
        NavigatorUtils.push(
            context, "${Routes.verifyCode}?phoneNum=${_nameController.text}&type=${widget.type}");

        return;
      }

      if (simCodeEntity.code == Api.ERROR_CODE ||
          simCodeEntity.code == Api.ERROR_CODE_0) {
        Navigator.pop(context);

        ToastUtil.showShortErrToast(simCodeEntity.message == ""
            ? Api.ERR_NOTHING
            : simCodeEntity.message);
        return;
      }
      Navigator.pop(context);
      ToastUtil.showShortErrToast(Api.ERR_NOTHING);
    },
        //错误回调
        (error) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      Navigator.pop(context);
    }, isToken: false);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _nameController.dispose();
  }
}
